package semantic_network;

import java.util.Vector;
import java.util.StringTokenizer;


public class Vector_Manipulation
{
    /*
     * This Class provides functions to manipulate Vectors objects.
     * @author Hammad Afzal
     */

    Vector vector1;

    /*
     * It converts a multi-word string into a vector. It also normalizes the string components
     * into lowercase
     * @param contents
     */
    public Vector keyword_to_vector(String str_to_convert, String seperator)
    {
        vector1 = new Vector();
        StringTokenizer st1 = new StringTokenizer(str_to_convert, seperator);
        while (st1.hasMoreElements())
        {
            String term = st1.nextToken();
            term.toLowerCase();
            term.trim();
            term = term.replaceAll("\r", "");
            term = term.trim();
            vector1.add(term);
        }
        return vector1;
    }

    /*
     * It converts a vector into a string that can be used in making query for database.
     * Example: Given vector V1={str1, str2}.
     *          Output String s = (str1,str2).
     */
    public String vector_to_string_for_making_db_query(Vector<String> vector_to_convert)
    {
        String str_list = vector_to_convert.toString();
        str_list = str_list.substring(1, str_list.length() - 1);
        str_list = "(" + str_list + ")";
        return str_list;
    }

    /*
     * It returns a vector of all contituents objects that comprise the lexical
     * profile of given string array.
     */
    public Vector make_term_profiles(String[] str)
    {
        Vector set1 = new Vector();
        String setElement;
        int i = 0;
        int j = 2;

        if (str.length == 1)
        {
            set1.add(str[0]);
            return set1;
        }
        else
        {
            try
            {
                while (i < str.length)
                {
                    set1.add(str[i]);
                    i++;
                }
                i = 0;
                while (j < str.length)
                {
                    while (j > i)
                    {
                        setElement = str[i] + " " + str[i + 1];
                        set1.add(setElement.toString());
                        i++;
                        setElement = "";
                    }
                    j++;
                }
            }
            catch (Exception ex)
            {
                System.out.println("Value of Exception in term profiles is : " + ex.getStackTrace());
            }
        }
        return set1;
    }

    /*
     * It returns a vector of all contituents objects that comprise the lexical
     * profile of given string.
     */
    public Vector make_term_profiles(String input_string)
    {
        Vector temp = new Vector();

        StringTokenizer tokenizer = new StringTokenizer(input_string, " ");

        while (tokenizer.hasMoreTokens())
        {
            temp.add(tokenizer.nextToken().toLowerCase());
        }

        String[] resource_constituents = (String[]) temp.toArray(new String[temp.size()]);
        Vector lexical_profile_vector = make_term_profiles(resource_constituents);
        return lexical_profile_vector;
    }

    /*
     * It sums all integer values stored in given vector, and returns the value as result.
     * @param int_vector A vector that contains integer values in string form.
     */
    public int sum_of_values_in_integer_vector(Vector<String> int_vector)
    {
        int total = 0;
        for (String freqs : int_vector)
        {
            freqs = freqs.replaceAll("\"", "");
            freqs = freqs.trim();
            int temp = Integer.parseInt(freqs);
            total = total + temp;
        }
        return total;
    }

        /*
     * Generate the lexical profile of a given string and returns a
     * Vector of strings which contains all components of lexical
     * profile
     */
    public Vector<String> make_term_profile(String term)
    {
        Vector<String> temp = new Vector();
        Vector<String> profile = new Vector();
        String[] temp_constituents;

        StringTokenizer tokenizer = new StringTokenizer(term, " ");
        while (tokenizer.hasMoreTokens())
        {
            temp.add(tokenizer.nextToken().toLowerCase());
        }
        temp_constituents = (String[]) temp.toArray(new String[temp.size()]);
        profile = make_term_profiles(temp_constituents);
        profile.add(term.toLowerCase());

        return profile;
    }


    /*
     * This method is vector assinger, and acts as simple "=" operator.
     */
    public Vector<String> vector_assigner(String vector_values_with_commas)
    {
        String str1 = vector_values_with_commas;
        str1 = str1.substring(1, str1.length() - 1);
        return keyword_to_vector(str1, ",");
    }


}
